/**
 * 
 */
var nextpage = 1;
var loading = false; //状态标记

/**
 * ajax表单提交，统一处理方法，附带可以显示输出效果
 */

var loading = "<i class='weui-loading'></i>";

var issubmit = "yes";
$("body").on("click", ".ajax-submit", function() {
	
	
	//提交出现加载
	$(".ajax-submit").hide();
	$(".sumbit_btn").append(loading);
//	$.alert(提交成功,请稍等);
	$("#uploaderFilesBox li").each(function() {
		var src = $(this).find("img").attr("src");
		$(this).find("input").attr("value", src);
	})
	
	var ajaxForm = $('form.ajax-form');
	var _url = ajaxForm.attr('action');
	if(issubmit == "yes"){
		$.ajax({
			type: 'post',
			url: _url,
			data: ajaxForm.serialize(),
			async:false,
			success: function(ret) {
				if(ret.status) {
					$(".login_btn").append(loading);
					$.toast(ret.info);
					setTimeout(function() {
						if(ret.url) {
							location.href = ret.url;
						} else {
							location.reload();
						}
					}, 2000);
					issubmit = "no";
				} else {
					$(".sumbit_btn .weui-loading").hide();
					$(".ajax-submit").show();
					$.alert(ret.info);
				}
			}
		});
	}
})






/**
 * 按钮 ajax提交
 */
$(document).on('click', '.ajax-get', function(event) {
	event.preventDefault();
	ajaxGet($(this).attr('href'));
});

/**
 * ajax get方式提交的核心代码
 * @param url
 */
function ajaxGet(_url, _data) {
	$.ajax({
		type: 'get',
		url: _url,
		data: _data,
		success: function(ret) {
			if(ret.status) {
				$.toast(ret.info);
				setTimeout(function() {
					if(ret.url) {
						location.href = ret.url;
					} else {
						location.reload();
					}
				}, 2000);
			} else {
				$.alert(ret.info);
			}
		}

	});
}

var _status = new Array();
_status[0] = '<span style="color:#23c0ff">待审核</span>';
_status[1] = '<span style="color:green">已通过</span>';
_status[2] = '<span style="color:red">已拒绝</span>';

var _stu_icon = new Array();
_stu_icon[0] = 'waiting';
_stu_icon[1] = 'success';
_stu_icon[2] = 'warn';

function showList(_page, _type) {
	$.ajax({
		url: "./index.php?g=&m=Apply&a=showlist",
		type: 'post',
		data: {
			page: _page,
			limit: 3,
			type: _type
		},
		beforeSend: function() {
			loading = true;
			$('#loading').css("display", "block");

		},
		success: function(ret) {
			// setTimeout(function() {
			if(ret.list == null) {
				$(document.body).destroyInfinite(); //销毁滚动加载
			} else {
				appendTemplate(ret.list);
				nextpage++;
			}
			$('#loading').css('display', 'none');
			loading = false;
			//  }, 150);   //模拟延迟
			
		

			//侧滑显示删除按钮
			var expansion = null; //是否存在展开的list
			var container = document.querySelectorAll('.slide');
			for(var i = 0; i < container.length; i++) {
				var x, y, X, Y, swipeX, swipeY;
				container[i].addEventListener('touchstart', function(event) {
					x = event.changedTouches[0].pageX;
					y = event.changedTouches[0].pageY;
					swipeX = true;
					swipeY = true;
					if(expansion) { //判断是否展开，如果展开则收起
						expansion.className = "weui-media-box weui-media-box_text";
					}
					var d = new Date;
	 				start_time = d.getTime();
				});
				container[i].addEventListener('touchmove', function(event) {
					X = event.changedTouches[0].pageX;
					Y = event.changedTouches[0].pageY;
					// 左右滑动
					if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0) {
						// 阻止事件冒泡
						event.stopPropagation();
						if(X - x > 10) { //右滑
							event.preventDefault();
							this.className = "weui-media-box weui-media-box_text"; //右滑收起
						}
						if(x - X > 10) { //左滑
							event.preventDefault();
							this.className = "swipeleft weui-media-box weui-media-box_text"; //左滑展开
							expansion = this;
						}
						swipeY = false;
					}
					// 上下滑动
					if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
						swipeX = false;
					}
				});
				container[i].addEventListener("touchend",function(){
					var d = new Date;
					end_time = d.getTime();
					if (end_time-start_time > 500) {
					  	event.preventDefault();
						this.className = "swipeleft weui-media-box weui-media-box_text"; //左滑展开
						expansion = this;
					}
				})
				$(".remove").click(function(){
					$(this).parent().remove();
				})
			}	
		}
	})

}
//第一层数据模板，申请信息
function appendTemplate(_data) {
	var tmp = '';
	for(var i in _data) {
		var data = _data[i];
		var audit = (data['audit'].length > 0) ? statusTemplate(data['audit']) : '';

		var time_interval = '';
		var status = '';
		if(data.start_time && data.end_time) {
			time_interval = data.start_time + '</br>' + data.end_time;
		} else {
			time_interval = data.start_time ? data.start_time : data.end_time;
		}
		var html = "";
		var slide = "";
		var id = data.id;
		var remove_btn = "";
		if(data.status == "0") {
			slide = "slide";
			remove_btn = "<a style='height:24px;font-size:17px;color:#ff3a32;line-height:24px;border-color:#ff3a32;margin:5px 15px 0 0' href='javascript:;' class='remove_btn ajax-get weui-btn weui-btn_plain-default fr'>删除</a>"
			html = "<div class='remove ajax-get' href='./index.php?g=&m=Apply&a=cancel&id="+id+"'><p>删除</p></div>";
		}
		var apply_reason = data.content ? '原因: ' + data.content : '';
		tmp += '<div class="weui-media-box weui-media-box_text ' + slide + '" style="width:120%;transition: all 0.3s linear;position:relative;">' +
			'<div style="margin-right:2%;width:78%;display:inline-block;">' +
			'<h4 class="weui-media-box__title" style="white-space:inherit">' +
			'<div class="fl" style="width: 70%;white-space: inherit;">	<span>' + time_interval + '</span></div>' +
			'<div class="fr"><span class="" style="color: #999;">' + _status[data.status] + '</span><i class="weui-icon-' + _stu_icon[data.status] + '" style="margin-top: -5px;"></i></div>' +
//			remove_btn +
			'</h4>' +	
			'<p class="weui-media-box__desc" style="margin-bottom: 10px;overflow: inherit;display:block;height: auto;">' +
			'类型: ' + data.type +
			'</p>' +
			'<p class="weui-media-box__desc" style="margin-bottom: 10px;overflow: inherit;display:block;height: auto;width:70%;">' +
			apply_reason +
			'</p>' +
			audit +
			'</div>' +
			html +
			'</div> ';
			
		$("body").on("click",".remove_btn",function(){
			var __this = $(this);
			$.confirm({
			  title: '确认删除？',
			  text: '您确定要删除该申请吗？',
			  onOK: function () {
			    //点击确认
			    __this.parent().parent().next().click();
			    __this.parent().parent().parent().remove();
			  },
			  onCancel: function () {
			  }
			});
		})
	}
	$('#list-box').append(tmp);
}

//第二层数据模板，流程信息
function statusTemplate(_data) {
		
	var tmp = '';
	for(var i in _data) {
		var data = _data[i];
		var _reject = data.reject_reason ? '理由: ' + data.reject_reason : '';
		tmp += '<p class="weui-media-box__desc" style="margin-bottom: 10px;overflow: inherit;display:block;height: auto;">' +
			'<span style="display: inline-block;width: 70%;">' + _reject + '</span>' +
			'<span style="text-align: right;" class="fr">' + data.approver_name + _status[data.app_status] + '</span>' +
			'</p>'
	}
	return tmp;
}